import { deviceAdd, deviceTypeList, deviceUpdate } from '@/services/deviceMsg';
import {
  ModalForm,
  ProFormSelect,
  ProFormText,
} from '@ant-design/pro-components';
import { Button, Col, Form, Row, message } from 'antd';
import React, { useImperativeHandle, useState } from 'react';

const ItemOption = React.forwardRef((props: any, ref) => {
  const { refreshList } = props;
  const [form] = Form.useForm<any>();
  const [modalOpen, setModalOpen] = useState(false);
  const [modItem, setModItem] = useState<any>(null);
  const [deviceTypeListData, setDeviceTypeListData] = useState<any>([]);
  const handleModalShow = async (row?: any) => {
    setModItem(row || null);
    if (!modalOpen) {
      form.resetFields();
    }
    if (row) {
      console.log(row);
      form.setFieldsValue({
        ...row,
      });
    }
    if (!modalOpen) {
      getDeviceTypeListData();
    }
    setModalOpen(!modalOpen);
  };
  const getDeviceTypeListData = async () => {
    const res = await deviceTypeList({
      pageSize: 1000,
      pageNum: 1,
    });
    if (res?.code === 200) {
      setDeviceTypeListData(
        res.rows.map((its: any) => {
          return {
            value: its.typeValue,
            label: its.name,
            deviceTypeId: its.id,
          };
        }),
      );
    }
  };
  useImperativeHandle(ref, () => ({
    handleModalShow,
  }));

  return (
    <ModalForm
      title={modItem?.id ? '修改设备' : '新增设备'}
      width={600}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnHidden: true,
        onCancel: handleModalShow,
        centered: true,
        maskClosable: false,
      }}
      open={modalOpen}
      submitter={{
        render: (props) => {
          let optBtns: any = [
            <Button
              className="base-button"
              key="cancel"
              onClick={handleModalShow}
            >
              取消
            </Button>,
            <Button
              className="base-button"
              type="primary"
              key="submit"
              onClick={() => props.form?.submit?.()}
            >
              确定
            </Button>,
          ];
          return optBtns;
        },
      }}
      onFinish={async (values) => {
        const sendData = {
          ...values,
          deviceTypeId: deviceTypeListData.filter((its: any) => {
            return its.value === values.type;
          })[0]?.deviceTypeId,
        };
        let res = null;
        if (modItem?.id) {
          //编辑
          res = await deviceUpdate({
            ...sendData,
            id: modItem.id,
          });
        } else {
          //添加
          res = await deviceAdd(sendData);
        }
        if (res?.code === 200) {
          message.success('操作成功');
          refreshList();
          setModalOpen(false);
        }
        return true;
      }}
    >
      <Row gutter={20}>
        <Col span={12}>
          <ProFormText
            name="name"
            label="设备名称"
            fieldProps={{
              maxLength: 100,
              showCount: true,
            }}
            rules={[
              {
                required: true,
                message: '请输入设备名称',
              },
            ]}
          />
        </Col>
        <Col span={12}>
          <ProFormSelect
            name="type"
            label="设备类型"
            placeholder="请选择设备类型"
            options={deviceTypeListData}
            rules={[
              {
                required: true,
                message: '请选择设备类型',
              },
            ]}
          />
        </Col>
      </Row>
      <Row gutter={20}>
        <Col span={12}>
          <ProFormText
            name="manufacturer"
            label="厂家"
            fieldProps={{
              maxLength: 100,
              showCount: true,
            }}
            rules={[
              {
                required: true,
                message: '请输入设备厂家',
              },
            ]}
          />
        </Col>
        <Col span={12}>
          <ProFormText
            name="productModel"
            label="型号"
            fieldProps={{
              maxLength: 100,
              showCount: true,
            }}
            rules={[
              {
                required: true,
                message: '请输入设备型号',
              },
            ]}
          />
        </Col>
      </Row>
      <Row gutter={20}>
        <Col span={24}>
          <ProFormText
            name="ip"
            label="IP地址"
            fieldProps={{
              maxLength: 200,
              showCount: true,
            }}
            rules={[
              {
                required: true,
                message: '请输入IP地址',
              },
            ]}
          />
        </Col>
      </Row>
      <Row gutter={20}>
        <Col span={24}>
          <ProFormText
            name="mac"
            label="MAC地址"
            placeholder="请输入MAC地址"
            fieldProps={{
              maxLength: 200,
              showCount: true,
            }}
          />
        </Col>
      </Row>
    </ModalForm>
  );
});

export default ItemOption;
